# coding: utf-8
from . import admin
from flask_login import login_required, current_user
from models.models import Post, Comment, User
from flask import render_template, flash, redirect, url_for, current_app
from ..blog.forms import blog_form
from .. import db


@admin.route('/manage/',defaults={'page':1})
@admin.route('/manage/<int:page>')
@login_required
def manage(page):
    user_id = current_user.id
    per_page = current_app.config['BLOG_POST_PER_PAGE']
    pagination = Post.query.filter_by(author_id=user_id)\
        .order_by(Post.timestamp.desc()).paginate(page, per_page=per_page)
    posts = pagination.items
    comments = []
    for i in posts:
        comments.append(Comment.query.filter_by(post_id=i.id)
                        .order_by(Comment.timestamp.desc()).all())
    data = zip(posts, comments)
    return render_template('manage.html',  data=data, pagination=pagination)


@admin.route('/operate/<int:post_id>',)
@login_required
def operate(post_id):
    post = Post.query.get(post_id)
    comments = Comment.query.filter_by(post_id=post_id)\
        .order_by(Comment.timestamp.desc()).all()
    form = blog_form()
    form.body.data = post.body
    form.title.data = post.title
    users = []
    for i in comments:
        users.append(User.query.get(i.author_id).username)
    data = zip(comments, users)
    return render_template('operate.html', form=form, data=data, post=post)


@admin.route('/delete/<int:comment_id>/<int:post_id>')
@login_required
def delete(comment_id, post_id):
    user_id1 = current_user.id
    user_id2 = Post.query.get(post_id).author_id
    if user_id1 != user_id2:
        flash("you can not delete others comment")
    else:
        comment = Comment.query.get(comment_id)
        comment.body = '该评论已被删除'
        db.session.commit()
        flash('delete success')
    return redirect(url_for('admin.operate', post_id=post_id))


@admin.route('/update/<int:post_id>', methods=['POST'])
@login_required
def update(post_id):
    user_id1 = current_user.id
    post = Post.query.get(post_id)
    user_id2 = post.author_id
    if user_id1 != user_id2:
        flash("you can not change others post")
    form = blog_form()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        db.session.commit()
        flash('修改博客文章成功')
    return redirect(url_for('admin.operate', post_id=post_id))


@admin.route('/my_comment/', defaults={'page': 1})
@admin.route('/my_comment/<int:page>')
@login_required
def my_comment(page):
    per_page = 10
    user_id = current_user.id
    pagination = Comment.query.filter_by(author_id=user_id) \
        .order_by(Comment.timestamp.desc()).paginate(page, per_page=per_page)
    comments = pagination.items
    posts = []
    for i in comments:
        posts.append(Post.query.get(i.post_id))
    data = zip(comments, posts)
    return render_template('my_comment.html', data=data, pagination=pagination)


@admin.route('/delete_comment/<int:comment_id>')
@login_required
def delete_comment(comment_id):
    user_id1 = current_user.id
    comment = Comment.query.get(comment_id)
    user_id2 = comment.author_id
    if user_id1 != user_id2:
        flash('you can not delete others comment')
    else:
        db.session.delete(comment)
        db.session.commit()
    return redirect(url_for('admin.my_comment'))